<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Leave extends CI_Controller {
 
	function __construct()
	{
		parent::__construct();		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{ 
		redirect('auth/login');
		}		
		$this->load->library('form_validation');
		$this->load->model('user/leave_model');	
		$this->load->model('user/attendance_model');				
	}
		
	function index()
	{		
		$data['total_leave_allocated'] = $this->leave_model->total_leaves();
		//$data['pending_leaves'] = $this->leave_model->get_pending_leaves();
		//$data['leave_status'] = $this->leave_model->get_leave_status();
		$data['leave_granted'] = $this->leave_model->total_leave_granted();
		$data['leave_remaning'] = $this->leave_model->leave_remaining();
		
		
		// getting the leaves applied
		
		 /*pagination starts here*/
		/*$pagination = array();
		$pagination['base_url'] = site_url('user/leave');
		$pagination['total_rows'] = count($this->leave_model->get_all_leaves());
		$pagination["uri_segment"] = 3;
		$pagination["per_page"] = 10;*/
		
         /*customizing the pagination*/   
			/*$pagination['prev_link'] = 'Prev';
            $pagination['prev_tag_open'] = '<li>';
            $pagination['prev_tag_close'] = '</li>';
            $pagination['next_link'] = 'Next';
            $pagination['next_tag_open'] = '<li>';
            $pagination['next_tag_close'] = '</li>';
            $pagination['cur_tag_open'] = '<li class="active"><a href="#">';
            $pagination['cur_tag_close'] = '</a></li>';
            $pagination['num_tag_open'] = '<li>';
            $pagination['num_tag_close'] = '</li>';	*/	
		
		  	//$this->pagination->initialize($pagination);		 
			//$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		   
		//$data["leaves"] = $this->leave_model->get_all_leaves($pagination["per_page"], $page)->result();
		  $data["leaves"] = $this->leave_model->get_all_leaves('pending')->result();
		  $data["leaves_verified"] = $this->leave_model->get_all_leaves('verified')->result();
		//  $data["links"] = $this->pagination->create_links();
		
		/*$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="table table-bordered">' );
		$this->table->set_template($tmpl);
		
		$data["lv"] = $this->table->generate($this->leave_model->get_all_leaves());*/
		
		$meta['page_title'] = 'Request Leave Page';
	 	$this->load->view('common/header',$meta);
	 	$this->load->view('user/request-leave',$data);
     	$this->load->view('common/footer');		
	}
	
   function apply_leave()
   {
 	 $meta['page_title'] = "Welcome To Leave Mangement ";
	  $edit_id = $this->input->post('edit_id');
	 	
		if(!empty($edit_id))
		{			
			$this->form_validation->set_rules('start_date', 'Date', 'required');			
			$this->form_validation->set_rules('reason', 'Reason', 'required');
			$this->form_validation->set_rules('edit_id', 'Id', 'required');			
		}else {
		$this->form_validation->set_rules('start_date', 'Date', 'required|callback_check_date');
		$this->form_validation->set_rules('reason', 'Reason', 'required');
		}

if ($this->form_validation->run() == FALSE)
	{
		// getting the leaves applied
		
		 /*pagination starts here*/
		/*$pagination = array();
		$pagination['base_url'] = site_url('user/leave');
		$pagination['total_rows'] = count($this->leave_model->get_all_leaves());
		$pagination["uri_segment"] = 3;
		$pagination["per_page"] = 10;*/
		
         /*customizing the pagination*/   
			/*$pagination['prev_link'] = 'Prev';
            $pagination['prev_tag_open'] = '<li>';
            $pagination['prev_tag_close'] = '</li>';
            $pagination['next_link'] = 'Next';
            $pagination['next_tag_open'] = '<li>';
            $pagination['next_tag_close'] = '</li>';
            $pagination['cur_tag_open'] = '<li class="active"><a href="#">';
            $pagination['cur_tag_close'] = '</a></li>';
            $pagination['num_tag_open'] = '<li>';
            $pagination['num_tag_close'] = '</li>';		
		
		  	$this->pagination->initialize($pagination);		 
			$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		   
		  $data["leaves"] = $this->leave_model->get_all_leaves($pagination["per_page"], $page)->result();
		  $data["links"] = $this->pagination->create_links();*/
		  	
		$data['total_leave_allocated'] = $this->leave_model->total_leaves();		
		$data['leave_granted'] = $this->leave_model->total_leave_granted();
		$data['leave_remaning'] = $this->leave_model->leave_remaining();
		
		$data["leaves"] = $this->leave_model->get_all_leaves('pending')->result();
		$data["leaves_verified"] = $this->leave_model->get_all_leaves('verified')->result();
		
		$meta['page_title'] = 'Request Leave Page';
	 	$this->load->view('common/header',$meta);
	 	$this->load->view('user/request-leave',$data);
     	$this->load->view('common/footer');	
			
	}else {
		
		if(!empty($edit_id))
		{
		$update_leave = array(
		'user_id' 	=> $this->session->userdata('user_id'),
		'from' 		=> $this->input->post('start_date'),
		'to' 		=> $this->input->post('end_date'),
		'reason' 	=> $this->input->post('reason'),
		);
			$this->leave_model->update($update_leave,$edit_id);
			$this->session->set_flashdata('leave_message', 'Leave Updated Successfully');
			redirect('user/leave','refresh');			
		}else {	
		$from 	= $this->input->post('start_date');
		$to		= $this->input->post('end_date');
		
		
		if(empty($to)){
			$leave = array(
		'user_id' 	=> $this->session->userdata('user_id'),		
		'reason' 	=> $this->input->post('reason'),
		'applied_on' => date('Y-m-d H:m:i'),
		'status' => 'pending',
		);
		$dates[] = $from;
			$this->leave_model->insert_user_leave($leave,$dates);			
		}else{	
						
		$r = range($from,$to);
		
		$datetime1 = date_create($from);
		$datetime2 = date_create($to);
		$interval = date_diff($datetime1, $datetime2);
		$date_difference = $interval->format('%a');					
		
		$leave = array(
		'user_id' 	=> $this->session->userdata('user_id'),		
		'reason' 	=> $this->input->post('reason'),
		'applied_on' => date('Y-m-d H:m:i'),
		'status' => 'pending',
		);		
		
		for($i=0;$i<=($date_difference);$i++){
			$dates[] = date('Y-m-d',strtotime($from . "+{$i} days"));
			}
		
		$this->leave_model->insert_user_leave($leave,$dates);	
		}
		$this->session->set_flashdata('leave_message', 'Leave Succesfully Applied');
		redirect('user/leave','refresh');		
		}
	}
		
   }
	
	function check_date($str)
	{	
	 $server_date = $this->attendance_model->get_server_date();
	
	if($str == $server_date)
	{
		$this->form_validation->set_message('check_date', 'You cannot apply leave for the same date');
		return FALSE;
	}
	elseif($this->leave_model->duplicate_date($str)>0)
	{
		$this->form_validation->set_message('check_date', 'You have already applied leave on this date.');
		return FALSE;
	}
	else {
		return TRUE;
	}
}

function delete($id)
{
$this->leave_model->delete($id);
$this->session->set_flashdata('leave_message', 'Leave Deleted Successfully');
redirect('user/leave','refresh');
}

function delete_particular($id)
{
$this->leave_model->delete_particular($id);
$this->session->set_flashdata('leave_message', 'Leave Deleted Successfully');
redirect('user/leave','refresh');
}

function edit($edit_id)
	{
		$data = $this->leave_model->edit($edit_id);		
		$data['edit'] = array(
		'id' => $data['id'],
		'start_date'=> $data['from'],
		'end_date'=> $data['to'],
		'reason' => $data['reason'],
		);		
		//$data['total_leave'] = $this->leave_model->total_leaves();
		//$data['pending_leaves'] = $this->leave_model->get_pending_leaves();
		//$data['leave_status'] = $this->leave_model->get_leave_status();
		//$data['leave_granted'] = $this->leave_model->total_leave_granted();
		$data['leave_remaning'] = 12 - $data['leave_granted'];
		$meta['page_title'] = 'Request Leave Page';
	 	$this->load->view('common/header',$meta);
	 	$this->load->view('user/request-leave',$data);
  		$this->load->view('common/footer');
	}
	
	
	
}
	
	?>